// testView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "test.h"

#include "testDoc.h"
#include "testView.h"
#include "Food.h"
#include "Snake.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CView)

BEGIN_MESSAGE_MAP(CTestView, CView)
	//{{AFX_MSG_MAP(CTestView)
	ON_WM_TIMER()
	ON_COMMAND(ID_VIEW_START, OnViewStart)
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{
	// TODO: add construction code here
	CPoint point;

	pSnake = new CSnake(2);
	pFood  = new CFood();
	pFood->MakePoint(point);
	pFood->SetPosition(point);
}

CTestView::~CTestView()
{
	//delete pSnake;
	//delete pFood;
}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

void CTestView::OnDraw(CDC* pDC)
{
	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	DrawBack(pDC);
	pSnake->Draw(pDC);

	if (pFood != NULL)
	{
		pFood->DrawFood(pDC);
	}

	if (pSnake->IsOver())
	{
		DrawOver(pDC);
		KillTimer(m_nTimer);
	}
}

void CTestView::DrawBack(CDC *pDC)
{
	pDC->Rectangle(50, 50, 450, 450);
}

void CTestView::DrawOver(CDC *pDC)
{
	pDC->TextOut(200, 200, "Game Over");
}

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
	return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers

void CTestView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CView::OnTimer(nIDEvent);

	Invalidate(TRUE);
	
	//if catch food, then eat it, and renew one
	pSnake->Move();

	if (pFood != NULL)
	{
		CPoint foodPos;
		CPoint snakeHead;
		CPoint newfoodPos;

		foodPos = pFood->GetPositon();
		snakeHead = pSnake->GetHead();
		if (foodPos.x == snakeHead.x && foodPos.y == snakeHead.y)
		{
			pSnake->EatFood();
			delete pFood;
			pFood = NULL;

			pFood  = new CFood();
			pFood->MakePoint(newfoodPos);
			pFood->SetPosition(newfoodPos);
		}
	}
	//TRACE("timer ...\n");
}

void CTestView::OnViewStart() 
{
	// TODO: Add your command handler code here
	m_nTimer = SetTimer(1, 200, 0);
}

void CTestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CView::OnKeyDown(nChar, nRepCnt, nFlags);

	TRACE("nChar = %d\n", nChar);
	
	switch(nChar)
	{
		case 37:
			if (pSnake->GetWay() != RIGHT)
				pSnake->SetWay(LEFT);
			break;
		case 38:
			if (pSnake->GetWay() != BOTTOM)
				pSnake->SetWay(TOP);
			break;
		case 39:
			if (pSnake->GetWay() != LEFT)
				pSnake->SetWay(RIGHT);
			break;
		case 40:
			if (pSnake->GetWay() != TOP)
				pSnake->SetWay(BOTTOM);
			break;
	}
}
